﻿using System;
using System.Collections.Generic;
using System.Text;
using SdmxMl.Manager;

namespace SdmxMl.Common
{
    /// <summary> Base class for typed Artefact reference </summary>
    public class ArtefactRefTyped : ArtefactRefBase
    {
        #region Properties

        // Used to create stubs info (isExternal)
        public MultiLang Name { get; set; }
        public bool IsFinal { get; set; }

        // Typed info
        public SdmxArtefactType Type{ get; set; }
        public bool IsLoaded { get; set; }

        /// <summary>Unique ID among all types od Artefact</summary>
        public string TypedId { get { return Type.ToString() + "+" + FullIdent; } }

        #endregion

        #region Constructors

        public ArtefactRefTyped(SdmxArtefactType type,
            string id, string agency, string vers)
            : base(id, agency, vers)
        {
            Type = type;
            IsLoaded = false;
        }

        public ArtefactRefTyped(string type,
            string id, string agency, string vers)
            : base(id, agency, vers)
        {
            Type = (SdmxArtefactType)Enum.Parse(typeof(SdmxArtefactType), type, true);
            IsLoaded = false;
        }

        public ArtefactRefTyped(SdmxArtefactType type, string fullIdent) 
        {
            // To signal argument error
            if (string.IsNullOrEmpty(fullIdent))
                fullIdent = "Null or Empty";

            Type = type;
            IsLoaded = false;
            string[] arr = fullIdent.Split('+');
            if (arr.Length != 3)
                throw new ApplicationException("Invalid Artefact FullId: " + fullIdent);
            Id = arr[1];
            AgencyId = arr[0];
            Version = arr[2];
        }

        #endregion
    }

    /// <summary>Class for managing a list of typed artefact references </summary>
    public class ArtefactRefTypedList : List<ArtefactRefTyped>
    {
        #region Properties

        public bool FullyLoaded
        {
            get
            {
                foreach (ArtefactRefTyped art in this)
                    if (art.IsLoaded == false)
                        return false;

                return true;
            }
        }

        /// <summary>Property for getting a missing artefact basic info message</summary>
        public string MissingListMessage
        {
            get
            {
                StringBuilder sb = new StringBuilder(1024);
                foreach (ArtefactRefTyped art in this)
                    if (art.IsLoaded == false)
                    {
                        sb.Append(art.Type.ToString()).Append(':');
                        sb.Append(art.FullIdent).Append(Environment.NewLine);
                    }
                return sb.ToString();
            }
        }
        #endregion

        #region Construction

        public ArtefactRefTypedList()
        {
        }
        public ArtefactRefTypedList(ArtefactRefTyped art)
        {
            Add(art);
        }


        #endregion

        #region Public Methods

        /// <summary>Look if specied reference is in container </summary>
        /// <param name="type">Artefact type</param>
        /// <param name="fullIdent">Artefact full ident</param>
        /// <returns>True if in list</returns>
        public bool Contains(SdmxArtefactType type, string fullIdent)
        {
            foreach (ArtefactRefTyped art in this)
                if (art.Type == type && art.FullIdent == fullIdent)
                    return true;
            return false;
        }
        #endregion
    }
}
